% Reads in an edi file
% latest date 17.4.3

stns = [1011,1012,2004,2010,2013,2016,2018,2020,3005,3006,3010,3014,3015,4001,4002,4003,4015,4017,4019];
%st_ch = ['11'; '12';'04';'10';'13';'16';'18';'20';'05';'06';'10';'14';'15';'01';'02';'03';'15';'17';'19'];
st_ch =(num2str(stns'))
clr = ['r.';'k.';'g.';'b*';'k*';'g*'];

ProcDef1.equations=[4 1 2;5 2 1];
ProcDef1.neq = 2;
ProcDef1.rreqn = [4 1 2 6 7;5 2 1 7 6];

path1 = 'c:\manoj\phd\rb-rm\';
s = dir(path1);

for i = 1:length(s)-2,
SS(i,:) = s(i+2).name;
end;


for i = 1:length(stns),
    fid = fopen(['c:\manoj\phd\procmt\' 'VP' st_ch(i,3:4) 'FL.EDI' ],'rt'); 
    
    if fid==-1,
        disp('Error');
        return;
    end;
        
    [SPM,er]=edi_in(fid);
    ProcDef1.nfrq=length(SPM.spectra);
    ProcDef1.TLFreq1=SPM.head.frq; 
    nchan = length(SPM.spectra(1).data);
    tf1 = TransFn(SPM,ProcDef1);
          
    [Rhxy,Rhyx,Phxy,Phyx] = tf2resph(tf1,ProcDef1);
    
    subplot(2,2,1);
    loglog(1./ProcDef1.TLFreq1,Rhxy,'b.');
    grid;
    hold on;
    
    subplot(2,2,2);
    loglog(1./ProcDef1.TLFreq1,Rhyx,'b.');
    grid;
    hold on;
    
    subplot(2,2,3);
    semilogx(1./ProcDef1.TLFreq1,Phxy,'b.');
    grid;
    hold on;
    
    subplot(2,2,4);
    semilogx(1./ProcDef1.TLFreq1,Phyx,'b.');
    grid;
    hold on;
    
    LL = str2num(SS(:,3:6)) == str2num(st_ch(i,:));
    files = SS(LL,:);
    disp(['c:\manoj\phd\procmt\' 'VP' st_ch(i,3:4) 'FL.EDI' ]);
    for l = 1:sum(LL),
        
        fid = fopen([path1 files(l,:)],'rt'); 
        disp([path1 files(l,:)]);
        [SPM,er]=edi_in(fid);
        ProcDef1.nfrq=length(SPM.spectra);
        ProcDef1.TLFreq1=SPM.head.frq; 
        nchan = length(SPM.spectra(1).data);
        tf1 = TransFn(SPM,ProcDef1);
        [Rhxy,Rhyx,Phxy,Phyx] = tf2resph(tf1,ProcDef1);
        
    
    subplot(2,2,1);
    loglog(1./ProcDef1.TLFreq1,Rhxy,clr(l,:));
    axis([1e-3 1e3 1e2 1e5]);
    hold on;
    
    subplot(2,2,2);
    loglog(1./ProcDef1.TLFreq1,Rhyx,clr(l,:));
    axis([1e-3 1e3 1e2 1e5]);
    hold on;
    
    subplot(2,2,3);
    semilogx(1./ProcDef1.TLFreq1,Phxy,clr(l,:));
    axis([1e-3 1e3 0 90]);
    hold on;
    
    subplot(2,2,4);
    semilogx(1./ProcDef1.TLFreq1,Phyx,clr(l,:));
    axis([1e-3 1e3 0 90]);
    hold on;
    end;
    title(['VP' st_ch(i,3:4)]);
    saveas(gcf,['c:\manoj\phd\images\' 'VP' files(1,5:6)],'bmp');
    disp(['c:\manoj\phd\images\' 'VP' files(1,5:6)]);
    clear files;
    clf;
%clear ProcDef1 ProcDef1;    
end;
